# -*- coding: iso-8859-1 -*-
"""
@newfield project: Project 
@project: yatter
@newfield description: Description 
@description: Twitter Client, based on Python 2.5

@Author: U{Andreas del Galdo<mailto:andreas.delgaldo@mac.com>}
@summary: The Main Module for loading the complete yatter-framework
          and get the connection to the twitter service.
"""

#import yatter framework
import yatter.tools.network
import yatter.twitter_api.interface
import yatter.twitter_api.tweet
import yatter.storage.store_disk
import yatter.storage.store_sqlite
import yatter.ui.mainView

#set logging
import logging
logging.basicConfig(level=logging.DEBUG)

#import python modules
import os

yatter.tools.network.configConnection("http://53.191.69.19", "8080", None, None)

global yatter_directory
yatter_directory = os.getcwd() + os.sep

yatter.ui.mainView.paintMainWindow(300, 500, 300, 300)

new_tweets = []
new_tweets = yatter.twitter_api.interface.createTweets(yatter.twitter_api.interface.getPublicTimeline())

db_connection = yatter.storage.store_sqlite.connectToDatabase(yatter_directory + "storage.yat")

for tweet in new_tweets:
    theTweet = yatter.twitter_api.tweet.tweet()

    theTweet = tweet
    user_image = yatter.storage.store_disk.loadImage(yatter_directory + 'cache',theTweet.getUser()['profile_image_url'])
    if (user_image == False):
        logging.debug("yatter.main.__init__()::CACHING_USER_IMAGE")
        if (yatter.storage.store_disk.storeImage(yatter_directory + 'cache', theTweet.getUser()['profile_image_url']) == True):
            user_image = yatter.storage.store_disk.loadImage(yatter_directory + 'cache',theTweet.getUser()['profile_image_url'])
    else:
        logging.debug("yatter.main.__init__()::CACHED_USER_IMAGE")
        
    #caching the image and saving the tweet in sqlite
    yatter.storage.store_sqlite.insertToDatabase(db_connection,"public_users", """INSERT INTO public_users VALUES ('%s','%s','%s','%s','%s','%s','%s','%s',%d)""" % 
                                                (theTweet.getUser()['id'], theTweet.getUser()['name'], theTweet.getUser()['screen_name'],
                                                 theTweet.getUser()['description'], theTweet.getUser()['location'], theTweet.getUser()['profile_image_url'],
                                                 theTweet.getUser()['url'], theTweet.getUser()['protected'], theTweet.getUser()['followers_count']))
    
    yatter.storage.store_sqlite.insertToDatabase(db_connection, "public_tweets", """INSERT INTO public_tweets VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s',%d)""" %
                                                (theTweet.getTweet()['id'], theTweet.getUser()['id'], theTweet.getTweet()['created_at'], theTweet.getTweet()['text'],
                                                 theTweet.getTweet()['source'], theTweet.getTweet()['trucated'], theTweet.getTweet()['in_reply_to_status_id'],
                                                 theTweet.getTweet()['in_reply_to_user_id'], theTweet.getTweet()['favorited']))
